home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Comm / www / tidy_os4.lha / tidy / src / istack.c < prev    next >
C/C++ Source or Header  |  2004-07-25  |  6KB  |  276 lines

  1. /* istack.c -- inline stack for compatibility with Mosaic
  2.  
  3.   (c) 1998-2003 (W3C) MIT, ERCIM, Keio University
  4.   See tidy.h for the copyright notice.
  5.   
  6.   CVS Info :
  7.  
  8.     $Author: creitzel $ 
  9.     $Date: 2003/09/26 13:28:02 $ 
  10.     $Revision: 1.14 $ 
  11.  
  12. */
  13.  
  14. #include "tidy-int.h"
  15. #include "lexer.h"
  16. #include "attrs.h"
  17. #include "streamio.h"
  18. #include "tmbstr.h"
  19.  
  20. extern Bool   debug_flag;
  21. extern Node  *debug_element;
  22. extern Lexer *debug_lexer;
  23.  
  24. /* duplicate attributes */
  25. AttVal *DupAttrs( TidyDocImpl* doc, AttVal *attrs)
  26. {
  27.     AttVal *newattrs;
  28.  
  29.     if (attrs == NULL)
  30.         return attrs;
  31.  
  32.     newattrs = NewAttribute();
  33.     *newattrs = *attrs;
  34.     newattrs->next = DupAttrs( doc, attrs->next );
  35.     newattrs->attribute = tmbstrdup(attrs->attribute);
  36.     newattrs->value = tmbstrdup(attrs->value);
  37.     newattrs->dict = FindAttribute(doc, newattrs);
  38.     return newattrs;
  39. }
  40.  
  41. /*
  42.   push a copy of an inline node onto stack
  43.   but don't push if implicit or OBJECT or APPLET
  44.   (implicit tags are ones generated from the istack)
  45.  
  46.   One issue arises with pushing inlines when
  47.   the tag is already pushed. For instance:
  48.  
  49.       <p><em>text
  50.       <p><em>more text
  51.  
  52.   Shouldn't be mapped to
  53.  
  54.       <p><em>text</em></p>
  55.       <p><em><em>more text</em></em>
  56. */
  57. void PushInline( TidyDocImpl* doc, Node *node)
  58. {
  59.     Lexer* lexer = doc->lexer;
  60.     IStack *istack;
  61.  
  62.     if (node->implicit)
  63.         return;
  64.  
  65.     if (node->tag == NULL)
  66.         return;
  67.  
  68.     if (!(node->tag->model & CM_INLINE))
  69.         return;
  70.  
  71.     if (node->tag->model & CM_OBJECT)
  72.         return;
  73.  
  74.     if ( !nodeIsFONT(node) && IsPushed(doc, node) )
  75.         return;
  76.  
  77.     /* make sure there is enough space for the stack */
  78.     if (lexer->istacksize + 1 > lexer->istacklength)
  79.     {
  80.         if (lexer->istacklength == 0)
  81.             lexer->istacklength = 6;   /* this is perhaps excessive */
  82.  
  83.         lexer->istacklength = lexer->istacklength * 2;
  84.         lexer->istack = (IStack *)MemRealloc(lexer->istack,
  85.                             sizeof(IStack)*(lexer->istacklength));
  86.     }
  87.  
  88.     istack = &(lexer->istack[lexer->istacksize]);
  89.     istack->tag = node->tag;
  90.  
  91.     istack->element = tmbstrdup(node->element);
  92.     istack->attributes = DupAttrs( doc, node->attributes );
  93.     ++(lexer->istacksize);
  94. }
  95.  
  96. /* pop inline stack */
  97. void PopInline( TidyDocImpl* doc, Node *node )
  98. {
  99.     Lexer* lexer = doc->lexer;
  100.     AttVal *av;
  101.     IStack *istack;
  102.  
  103.     if (node)
  104.     {
  105.         if (node->tag == NULL)
  106.             return;
  107.  
  108.         if (!(node->tag->model & CM_INLINE))
  109.             return;
  110.  
  111.         if (node->tag->model & CM_OBJECT)
  112.             return;
  113.  
  114.         /* if node is </a> then pop until we find an <a> */
  115.         if ( nodeIsA(node) )
  116.         {
  117.             while (lexer->istacksize > 0)
  118.             {
  119.                 --(lexer->istacksize);
  120.                 istack = &(lexer->istack[lexer->istacksize]);
  121.  
  122.                 while (istack->attributes)
  123.                 {
  124.                     av = istack->attributes;
  125.                     istack->attributes = av->next;
  126.                     FreeAttribute( doc, av );
  127.                 }
  128.  
  129.                 if ( istack->tag->id == TidyTag_A )
  130.                 {
  131.                     MemFree(istack->element);
  132.                     break;
  133.                 }
  134.  
  135.                 MemFree(istack->element);
  136.             }
  137.  
  138.             return;
  139.         }
  140.     }
  141.  
  142.     if (lexer->istacksize > 0)
  143.     {
  144.         --(lexer->istacksize);
  145.         istack = &(lexer->istack[lexer->istacksize]);
  146.  
  147.         while (istack->attributes)
  148.         {
  149.             av = istack->attributes;
  150.             istack->attributes = av->next;
  151.             FreeAttribute( doc, av );
  152.         }
  153.  
  154.         MemFree(istack->element);
  155.  
  156.         /* #427822 - fix by Randy Waki 7 Aug 00 */
  157.         if (lexer->insert >= lexer->istack + lexer->istacksize)
  158.             lexer->insert = NULL;
  159.     }
  160. }
  161.  
  162. Bool IsPushed( TidyDocImpl* doc, Node *node)
  163. {
  164.     Lexer* lexer = doc->lexer;
  165.     int i;
  166.  
  167.     for (i = lexer->istacksize - 1; i >= 0; --i)
  168.     {
  169.         if (lexer->istack[i].tag == node->tag)
  170.             return yes;
  171.     }
  172.  
  173.     return no;
  174. }
  175.  
  176. /*
  177.   This has the effect of inserting "missing" inline
  178.   elements around the contents of blocklevel elements
  179.   such as P, TD, TH, DIV, PRE etc. This procedure is
  180.   called at the start of ParseBlock. when the inline
  181.   stack is not empty, as will be the case in:
  182.  
  183.     <i><h1>italic heading</h1></i>
  184.  
  185.   which is then treated as equivalent to
  186.  
  187.     <h1><i>italic heading</i></h1>
  188.  
  189.   This is implemented by setting the lexer into a mode
  190.   where it gets tokens from the inline stack rather than
  191.   from the input stream.
  192. */
  193. int InlineDup( TidyDocImpl* doc, Node* node )
  194. {
  195.     Lexer* lexer = doc->lexer;
  196.     int n;
  197.  
  198.     if ((n = lexer->istacksize - lexer->istackbase) > 0)
  199.     {
  200.         lexer->insert = &(lexer->istack[lexer->istackbase]);
  201.         lexer->inode = node;
  202.     }
  203.  
  204.     return n;
  205. }
  206.  
  207. /*
  208.  defer duplicates when entering a table or other
  209.  element where the inlines shouldn't be duplicated
  210. */
  211. void DeferDup( TidyDocImpl* doc )
  212. {
  213.     doc->lexer->insert = NULL;
  214.     doc->lexer->inode = NULL;
  215. }
  216.  
  217. Node *InsertedToken( TidyDocImpl* doc )
  218. {
  219.     Lexer* lexer = doc->lexer;
  220.     Node *node;
  221.     IStack *istack;
  222.     uint n;
  223.  
  224.     /* this will only be NULL if inode != NULL */
  225.     if (lexer->insert == NULL)
  226.     {
  227.         node = lexer->inode;
  228.         lexer->inode = NULL;
  229.         return node;
  230.     }
  231.  
  232.     /*
  233.     
  234.       is this is the "latest" node then update
  235.       the position, otherwise use current values
  236.     */
  237.  
  238.     if (lexer->inode == NULL)
  239.     {
  240.         lexer->lines = doc->docIn->curline;
  241.         lexer->columns = doc->docIn->curcol;
  242.     }
  243.  
  244.     node = NewNode(lexer);
  245.     node->type = StartTag;
  246.     node->implicit = yes;
  247.     node->start = lexer->txtstart;
  248.     /* #431734 [JTidy bug #226261 (was 126261)] - fix by Gary Peskin 20 Dec 00 */ 
  249.     node->end = lexer->txtend; /* was : lexer->txtstart; */
  250.     istack = lexer->insert;
  251.  
  252. #if 0 && defined(_DEBUG)
  253.     if ( lexer->istacksize == 0 )
  254.         fprintf( stderr, "0-size istack!\n" );
  255. #endif
  256.  
  257.     node->element = tmbstrdup(istack->element);
  258.     node->tag = istack->tag;
  259.     node->attributes = DupAttrs( doc, istack->attributes );
  260.  
  261.     /* advance lexer to next item on the stack */
  262.     n = (uint)(lexer->insert - &(lexer->istack[0]));
  263.  
  264.     /* and recover state if we have reached the end */
  265.     if (++n < lexer->istacksize)
  266.         lexer->insert = &(lexer->istack[n]);
  267.     else
  268.         lexer->insert = NULL;
  269.  
  270.     return node;
  271. }
  272.  
  273.  
  274.  
  275.  
  276.